﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no" />
<link rel="shortcut icon" href="Images/ico/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<link type="text/css" rel="stylesheet" href="/layui/css/layui.css"  media="all">
<script type="text/javascript" src="/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/layui-post.js"></script>
</head>
<body style="position:relative;">
    <div class="ng-scope">
        <header class="ng-scope" style="position:relative;" onclick="insertBachCart()">
            <h4 class="title-order">
                <a class="title-top" href="javascript:window.history.back();"></a>
                <a class="title-center">热食</a>
                <a class="title-right" href="/index"></a>
            </h4>
        </header>
        <div class="ng-scope">
            <img alt="" src="/images/lg.jpg" width="100%">
        </div>
        <ul class="lists lists-tof ng-scope" id="goods"></ul>
    </div>
    <!--底部结算导航-->
    <div class="cart ng-scope">
        <div class="r"><a href="/cart" onclick="insertBachCart()">去结算</a></div>
        <div class="l ng-binding">
            ￥<label id="totalmoney">0</label>
        </div>
        <i id="totalcartnumber" class="ng-binding cartnum">0</i>
        <a href="/cart" onclick="insertBachCart()"><img src="img/shop_cart01.png" alt="..."></a>
    </div>
    <!--/底部结算导航-->
</body>
</html>
<script>
$.ajaxSetup({contentType: "application/json; charset=utf-8"});
var goodsIds=[],reqdataList=[],totalcartnumber = 0,totalmoney = 0;
var userInfo = $.parseJSON(sessionStorage.getItem("userInfo"));
layui.use(['layer','form','element','util'], function(){
    var loading = layer.load();
    $.post("/cateringGoods/select",JSON.stringify({status:0}),function(res){
       $("#goods").html("")
        layer.close(loading)
        for(var i in res.data){
            $("#goods").append(
                '<li class="on" price="'+res.data[i].goodsPrice+'" goodsId="'+res.data[i].sysId+'" cart="2">' +
                '<div class="img view" onclick="window.location.href=\'/detail?sysId='+res.data[i].sysId+'\'"><img style="border-radius: 10px;" class="imggg" src="'+res.data[i].goodsImg+'" alt="..." /></div>' +
                '<p class="t">'+res.data[i].goodsName+'</p>' +
                '<p class="price">￥'+res.data[i].goodsPrice+'</p>' +
                '<div class="showaddcart meshop">' +
                '<div class="num_con">' +
                '<img src="img/num_l.png" class="img-responsive num_l" alt="...">' +
                '<span class="number" style="padding:10px;" id="'+res.data[i].sysId+'">0</span>' +
                '<img src="img/num_r.png" class="img-responsive num_r" alt="...">' +
                '</div>' +
                '</div>' +
                '</li>'
            )
        }
        $("#goods .imggg").css("height",window.innerWidth/2+"px");
    }).done(function(){
        $.post("/cateringCart/select",JSON.stringify({userId:userInfo.userId}),function(res){
            reqdataList = [];goodsIds = [];
            for(var i in res.data){
                totalcartnumber += res.data[i].goodsNum
                totalmoney += res.data[i].goodsNum*res.data[i].goodsPrice
                $("#"+res.data[i].goodsId).text(res.data[i].goodsNum)
                //将数据放入购物车
                if(goodsIds.indexOf(res.data[i].goodsId) === -1) {
                    let resdata = {};
                    goodsIds.push(res.data[i].goodsId)
                    resdata.goodsId = res.data[i].goodsId;
                    resdata.goodsNum = res.data[i].goodsNum
                    resdata.userId = userInfo.userId
                    reqdataList.push(resdata);
                }
            }
            $("#totalcartnumber").text(totalcartnumber);
            $("#totalmoney").text(totalmoney.toFixed(2));
        })
    })

    //shop：加入购物车
    $(".lists li").each(function (i, model) {
        var objLi = $(this);
        //点击减数量
        $(objLi).find(".num_l").click(function () {
            var goodsId =$(objLi).attr("goodsId");
            var number = parseInt($(objLi).find(".number").text()) - 1;
            var cart = $(objLi).attr("cart");
            if (number > 0) {
                $(objLi).find(".number").text(number);
                if(goodsIds.indexOf(goodsId) > -1){
                    for(var i in reqdataList){
                        if(reqdataList[i].goodsId === goodsId) reqdataList[i].goodsNum = number;
                    }
                }
            } else {
                if(goodsIds.indexOf(goodsId) > -1) {
                    goodsIds.splice($.inArray(goodsId,goodsIds),1)
                    reqdataList.splice($.inArray(goodsId,reqdataList),1)
                }
                if (cart == 1) {//说明是购物车中的操作
                    $(objLi).remove();
                } else if (cart == 2) {//说明是外卖的操作
                    if (number == 0) {
                        $(objLi).find(".number").text(number);
                    }
                } else {
                    $(objLi).removeClass("on");
                }
            }
            CountNumPrice();
        });
        //点击加数量
        $(objLi).find(".num_r").click(function () {
            var goodsId =$(objLi).attr("goodsId");
            var reqdata={};
            var number = parseInt($(objLi).find(".number").text()) + 1;
            if(goodsIds.indexOf(goodsId) === -1) {
                goodsIds.push(goodsId)
                reqdata.goodsId = goodsId;
                reqdata.goodsNum = number
                reqdata.userId = userInfo.userId
                reqdataList.push(reqdata);
            }else {
                for(var i in reqdataList){
                    if(reqdataList[i].goodsId === goodsId) reqdataList[i].goodsNum = number;
                }
            }
            $(objLi).find(".number").text(number);
            CountNumPrice();
        });
        //点击查看产品详情
        $(objLi).find(".view").click(function () {
            $(objLi).find(".detail").show(200);
        });
    });
})
function insertBachCart(){
    $.post("/cateringCart/insertBach",JSON.stringify({userId:userInfo.userId,cartslist:reqdataList}))
}
function CountNumPrice() {
    var totalcartnumber = 0;//数量
    var totalmoney = 0;//价格
    $(".lists .on").each(function (i, model) {
        var objLi = $(this);
        var number = parseInt($(objLi).find(".number").text());
        var price = parseFloat($(objLi).attr("price"));
        totalcartnumber = totalcartnumber + number;
        totalmoney = totalmoney + (number * price);
    });
    $("#totalcartnumber").text(totalcartnumber);
    $("#totalmoney").text(totalmoney.toFixed(2));
}
</script>